#!/var/bin/python
# -*- coding: utf-8 -*-
#######################
#	获取企查查黄页信息
#######################

import sys
import time
import urllib
import re
from bs4 import BeautifulSoup
import io
import os
dirs = os.path.abspath(os.path.dirname(__file__)+"/../Config")
os.sys.path.append(dirs)   #将上上级目录加载到python的环境变量中
# os.sys.path.append("D:/job/crawler/Config")
from config import conn,driver,logpath
from selenium.webdriver.common.keys import Keys  #selenium 输入需要引入keys包
driver.implicitly_wait(60)

print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),'Start crawling the data. . .')

cur = conn.cursor()
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),"Open your browser. . .")


# 打开登陆页面
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "Open the login page. . .")
driver.get("https://graph.qq.com/oauth/show?which=Login&display=pc&response_type=code&client_id=101188807&redirect_uri=http%3A%2F%2Fwww.qichacha.com%2Fuser_callbackqq&state=3daff0f5935d33e95facd2033cdbb312&scope=get_user_info,add_share")
driver.switch_to_frame('ptlogin_iframe')

print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "Switch to the use of account password login. . .")
driver.find_element_by_id('switcher_plogin').click()
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "Enter QQ account password. . .")
driver.find_element_by_id("u").clear()
driver.find_element_by_id("u").send_keys("2252875088")
driver.find_element_by_id("p").clear()
driver.find_element_by_id("p").send_keys("123698745")
driver.find_element_by_id("login_button").click()
driver.get("http://www.qichacha.com/gongsi_area_prov_AH_p_1.shtml")

soup = BeautifulSoup(driver.page_source,"html.parser")
alist = soup.find("dl",class_="filter-tag").find_all("a")
#企业列表
for a in alist:
	driver.get("http://www.qichacha.com/%s" % a.get('href')) 
	soup = BeautifulSoup(driver.page_source, "html.parser")
	enterpriselist = soup.find_all("section", id="searchlist")
	for enterprise in enterpriselist:
		enterprisea = enterprise.find("a")
		jsa = 'window.open("%s")' % enterprisea.get('href')
		driver.execute_script(jsa)
		handles = driver.window_handles  # 获取当前窗口句柄集合（列表类型）
		driver.switch_to_window(handles[-1])

		soup = BeautifulSoup(driver.page_source, "html.parser")
		base_lis = soup.find("ul", class_="company-base").find_all("li")

		base = {}
		for base_li in base_lis:
			base_liarr = base_li.text.split("：")
			base[base_liarr[0].strip()] = base_liarr[1].strip()

		fullname = soup.find("span", class_="text-big").text.strip()
		# 判断是否已经存在数据
		sqls = "select * from enterpriseinfo where ename like '%s'" % ("%" + fullname + "%")
		cur.execute(sqls)
		result = cur.fetchone()
		print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "data processing. . .")
		if result:
			sql = "update enterpriseinfo set `searchname`='%s',`ename`='%s',`creditcode`='%s',`regnumber`='%s',`organizationcode`='%s',`managementforms`='%s',`companytype`='%s',`RegisterDate`='%s',`legalperson`='%s',`registeredcapital`='%s',`busnissallotedtime`='%s',`registeroffice`='%s',`dateofissue`='%s',`scale`='%s',`industry`='%s',`Englishname`='%s',`businessaddress`='%s',`Businessscope`='%s',`status`=1 where eid=%d" % (
			"", fullname, base.get("统一社会信用代码", ""), base.get("注册号", ""), base.get("组织机构代码", ""),
			base.get("经营状态", ""), base.get("公司类型", ""), base.get("成立日期", ""), base.get("法定代表人", ""),
			base.get("注册资本", ""), base.get("营业期限", ""), base.get("登记机关", ""), base.get("发照日期", ""),
			base.get("公司规模", ""), base.get("所属行业", ""), base.get("英文名", ""), base.get("企业地址", ""), base.get("经营范围", ""),
			result[0])
		else:
			sql = "insert into enterpriseinfo (`searchname`,`ename`,`creditcode`,`regnumber`,`organizationcode`,`managementforms`,`companytype`,`RegisterDate`,`legalperson`,`registeredcapital`,`busnissallotedtime`,`registeroffice`,`dateofissue`,`scale`,`industry`,`Englishname`,`businessaddress`,`Businessscope`,`status`) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',%d)" % (
			"", fullname, base.get("统一社会信用代码", ""), base.get("注册号", ""), base.get("组织机构代码", ""),
			base.get("经营状态", ""), base.get("公司类型", ""), base.get("成立日期", ""), base.get("法定代表人", ""),
			base.get("注册资本", ""), base.get("营业期限", ""), base.get("登记机关", ""), base.get("发照日期", ""),
			base.get("公司规模", ""), base.get("所属行业", ""), base.get("英文名", ""), base.get("企业地址", ""), base.get("经营范围", ""),
			1)
		print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "Add company basic data to the database")
		try:
			cur.execute(sql)
			conn.commit()
			driver.close()
		except Exception as e:
			print("Failed to add data...", sql, e, name)
			driver.close()
		handles = driver.window_handles  # 获取当前窗口句柄集合（列表类型）
		driver.switch_to_window(handles[-1])
# driver.quit()